﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KKU.RBSFPP.Shared.DAC;

namespace KKU.RBSFPP.Web.Administrator.Controllers
{
    public class GraphsController : Controller
    {
        private RBSFPPModelContainer db = new RBSFPPModelContainer();

        //
        // GET: /Graphs/

        public ActionResult Index()

        {
            var now = DateTime.Today;
            var factorvalues = from it in db.FactorValues.Include("Factor")
                               where it.CreateOn.Year == now.Year
                               group it by it.CreateOn.Date into newfactor
                               select newfactor;
                
            
            return View(factorvalues.ToList());
        }

        //
        // GET: /Graphs/Details/5

        public ActionResult Details(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            return View(factorvalue);
        }

        //
        // GET: /Graphs/Create

        public ActionResult Create()
        {
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName");
            return View();
        }

        //
        // POST: /Graphs/Create

        [HttpPost]
        public ActionResult Create(FactorValue factorvalue)
        {
            if (ModelState.IsValid)
            {
                db.FactorValues.AddObject(factorvalue);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // GET: /Graphs/Edit/5

        public ActionResult Edit(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // POST: /Graphs/Edit/5

        [HttpPost]
        public ActionResult Edit(FactorValue factorvalue)
        {
            if (ModelState.IsValid)
            {
                db.FactorValues.Attach(factorvalue);
                db.ObjectStateManager.ChangeObjectState(factorvalue, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FactorId = new SelectList(db.Factors, "FactorId", "FactorName", factorvalue.FactorId);
            return View(factorvalue);
        }

        //
        // GET: /Graphs/Delete/5

        public ActionResult Delete(int id = 0)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            if (factorvalue == null)
            {
                return HttpNotFound();
            }
            return View(factorvalue);
        }

        //
        // POST: /Graphs/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            FactorValue factorvalue = db.FactorValues.Single(f => f.FactorValueId == id);
            db.FactorValues.DeleteObject(factorvalue);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}